package cn.myfirefly.moming.house.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.myfirefly.moming.house.dao.SpfTradePerformanceDao;
import cn.myfirefly.moming.house.entity.SpfTradePerformance;
import cn.myfirefly.moming.house.model.SpfTradePerformanceDto;
import cn.myfirefly.moming.house.model.SpfTradePerformanceFilterDto;
import cn.myfirefly.core.Const;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.utils.BeanUtil;
import cn.myfirefly.utils.StrUtil;

@Service
@Transactional
public class SpfTradePerformanceService {

	@Autowired
	private SpfTradePerformanceDao spfTradePerformanceDao;
	
//	@Autowired
//	private SpfTradeContractDao spfTradeContractDao;
	
	@SuppressWarnings("rawtypes")
	public PageResultDto getList(SpfTradePerformanceFilterDto dto) {
		
		return spfTradePerformanceDao.getList(dto);
	}
	
	public SpfTradePerformanceDto getSpfTradePerformance(Integer id) {
		SpfTradePerformance spfTradePerformance = spfTradePerformanceDao.get(id);
		SpfTradePerformanceDto spfTradePerformanceDto = BeanUtil.copyBeans(SpfTradePerformanceDto.class, spfTradePerformance);
		return spfTradePerformanceDto;
	}
	
	public SpfTradePerformanceDto getByContractId(Integer contractId) {
		SpfTradePerformance spfTradePerformance = spfTradePerformanceDao.getByContractId(contractId);
		SpfTradePerformanceDto spfTradePerformanceDto = BeanUtil.copyBeans(SpfTradePerformanceDto.class, spfTradePerformance);
		return spfTradePerformanceDto;
	}
	
	public String del(Integer id) {
		SpfTradePerformance spfTradePerformance = spfTradePerformanceDao.get(id);
		if (spfTradePerformance == null) {
			return "该信息不存在，请尝试刷新列表后再操作。";
		}
		spfTradePerformanceDao.del(spfTradePerformance);
		return Const.ResultCode.SUCCESS;
	}
	
	public Integer insert(SpfTradePerformanceDto dto) {
		SpfTradePerformance spfTradePerformance = BeanUtil.copyBeans(SpfTradePerformance.class, dto);
		return spfTradePerformanceDao.save(spfTradePerformance);
	}
	
	public Integer update(SpfTradePerformanceDto dto) {
		SpfTradePerformance spfTradePerformance = spfTradePerformanceDao.get(dto.getId());
		if (spfTradePerformance == null) {
			return -1;
		}
		spfTradePerformance.setGrantType(dto.getGrantType());
		if (StrUtil.equals(dto.getGrantType(), "1")) {
			spfTradePerformance.setCommissionAccountDate(dto.getCommissionAccountDate());
			spfTradePerformance.setUnwebGrantDate(dto.getUnwebGrantDate());
			spfTradePerformance.setGrantPercent(dto.getGrantPercent());
		} else if (StrUtil.equals(dto.getGrantType(), "2")) {
			spfTradePerformance.setWebSignDate(dto.getWebSignDate());
			spfTradePerformance.setReissueDate(dto.getReissueDate());
		} else if (StrUtil.equals(dto.getGrantType(), "3")) {
			spfTradePerformance.setCommissionAccountDate(dto.getCommissionAccountDate2());
			spfTradePerformance.setAllGrantDate(dto.getAllGrantDate());
		}
		
		int count = spfTradePerformanceDao.save(spfTradePerformance);
		
//		SpfTradeContract spfTradeContract = spfTradeContractDao.get(spfTradePerformance.getTradeContractId());
//		spfTradeContract.setTradeContractStatus("5");
//		spfTradeContractDao.save(spfTradeContract);
		return count;
	}
}
